/**
 * 
 */
package com.kathez.dao;

import java.util.List;

import com.kathez.entity.DAOOperable;
import com.kathez.entity.KathezEntity;
import com.kathez.exceptions.KathezRuntimeException;

/**
 * @author Balaji T
 *
 */
public final class DAOInsertResultImpl extends AbstractDAOInsertResult {

	/* (non-Javadoc)
	 * @see com.kathez.dao.DAOInsertResult#getInsertCount()
	 */
	@Override
	public int getInsertCount() {
		return this.insertCount;
	}

	/* (non-Javadoc)
	 * @see com.kathez.dao.DAOInsertResult#getInsertedObject()
	 */
	@Override
	public DAOOperable getInsertedObject() {
		return this.insertedObject;
	}

	/* (non-Javadoc)
	 * @see com.kathez.dao.DAOInsertResult#getInsertedObjectList()
	 */
	@Override
	public List<KathezEntity> getInsertedObjectList() {
		return this.insertedObjectList;
	}

	/* (non-Javadoc)
	 * @see com.kathez.dao.DAOInsertResult#getInsertErrCode()
	 */
	@Override
	public String getInsertErrCode() {
		return this.insertErrCode;
	}

	/* (non-Javadoc)
	 * @see com.kathez.dao.DAOInsertResult#getInsertErrMsg()
	 */
	@Override
	public String getInsertErrMsg() {
		return this.insertErrMsg;
	}

	/* (non-Javadoc)
	 * @see com.kathez.dao.DAOInsertResult#isInsertSucces()
	 */
	@Override
	public boolean isInsertSuccess() {
		return this.insertSuccess;
	}
	
	private boolean insertSuccess;
	private String insertErrMsg;
	private String insertErrCode;
	private List<KathezEntity> insertedObjectList;
	private DAOOperable insertedObject;
	private int insertCount;
	private boolean overrideSuccesFlag;
	/**
	 * @param insertSuccess the insertSuccess to set
	 */
	public void setInsertSuccess(boolean insertSuccess) {
		this.overrideSuccesFlag = true;
		this.insertSuccess = insertSuccess;
	}

	/**
	 * @param insertErrMsg the insertErrMsg to set
	 */
	public void setInsertErrMsg(String insertErrMsg) {
		this.insertErrMsg = insertErrMsg;
	}

	/**
	 * @param insertErrCode the insertErrCode to set
	 */
	public void setInsertErrCode(String insertErrCode) {
		this.insertErrCode = insertErrCode;
	}

	/**
	 * @param insertedObjectList the insertedObjectList to set
	 */
	public void setInsertedObjectList(List<KathezEntity> insertedObjectList) {
		if(insertedObjectList != null){
			if(this.insertedObject != null)
				throw new KathezRuntimeException("The Result already set for this object. The existing result is Single Inserted object");
			this.insertedObjectList = insertedObjectList;
			setInsertCount(insertedObjectList.size());
			if(overrideSuccesFlag && !this.insertSuccess)
				this.insertSuccess = false;
			else
				this.insertSuccess = true;
		}
		this.overrideSuccesFlag = false;
	}

	/**
	 * @param insertedObject the insertedObject to set
	 */
	public void setInsertedObject(DAOOperable insertedObject) {
		if(insertedObject != null){
			if(this.insertedObjectList != null)
				throw new KathezRuntimeException("The Result already set for this object. The existing result is List of Inserted objects");
			this.insertedObject = insertedObject;
			setInsertCount(1);
			if(overrideSuccesFlag && !this.insertSuccess)
				this.insertSuccess = false;
			else
				this.insertSuccess = true;
		}
		this.overrideSuccesFlag = false;
	}

	/**
	 * @param insertCount the insertCount to set
	 */
	private void setInsertCount(int insertCount) {		
		this.insertCount = insertCount;
	}
	
}
